package com.molichuxing.services.business.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import com.molichuxing.services.business.service.RegionBizService;
import com.molichuxing.services.business.service.SubCompanyBizService;
import com.molichuxing.services.infrastructure.dto.response.RegionCityDto;
import com.molichuxing.services.infrastructure.dto.response.RegionSubcompanyDto;
import com.molichuxing.services.infrastructure.dto.response.SubcompanyDto;
import com.molichuxing.services.infrastructure.service.RegionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.molichuxing.services.infrastructure.dto.response.RegionDto;

/**
 * <p>
 * 行政区域 服务实现类
 * </p>
 *
 * @author zoumingyu
 * @since 2019-07-28
 */
@Service("regionBizService")
public class RegionBizServiceImpl  implements RegionBizService {
	private static final Logger logger = LoggerFactory.getLogger(RegionBizServiceImpl.class);

	@Resource
	private RegionService regionService;

	@Resource
	private SubCompanyBizService subCompanyBizService;

	/**
	 * 获取所有省市区信息
	 * 
	 * @return
	 */
	@Override
	public List<RegionDto> getAll() {
		return regionService.getAll();
	}

	@Override
	public List<RegionCityDto> getAllCity(Integer level) {
		return regionService.getAllCity(level);
	}

	@Override
	public List<RegionSubcompanyDto> getProvinceSubCompany() {
		List<RegionSubcompanyDto> result = new ArrayList<>();
		List<RegionCityDto> provinceList = regionService.getAllCity(1);
		for (int i = provinceList.size() - 1; i >= 0; i--) {
			RegionCityDto regionCity = provinceList.get(i);
			List<SubcompanyDto> subcompanyDtoList = subCompanyBizService.getSubcompanyByCityId(regionCity.getId());
			if((null != subcompanyDtoList)&&(subcompanyDtoList.size() > 0)) {
				RegionSubcompanyDto regionSubCompany = new RegionSubcompanyDto();
				regionSubCompany.setRegionId(regionCity.getId());
				regionSubCompany.setName(regionCity.getName());
				regionSubCompany.setSubCompanyDto(subcompanyDtoList);
				result.add(regionSubCompany);
			}
		}
		return result;
	}
}
